package com.ruoyi.SSL.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.utils.msg.EmailUtils;
import com.ruoyi.utils.msg.QyWxWebhookUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.SSL.mapper.SslNotificationLogMapper;
import com.ruoyi.SSL.domain.SslNotificationLog;
import com.ruoyi.SSL.service.ISslNotificationLogService;
import com.ruoyi.common.core.text.Convert;

/**
 * 消息提醒记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class SslNotificationLogServiceImpl implements ISslNotificationLogService
{
    @Autowired
    private SslNotificationLogMapper sslWarmLogMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询消息提醒记录
     *
     * @param id 消息提醒记录主键
     * @return 消息提醒记录
     */
    @Override
    public SslNotificationLog selectSslWarmLogById(Long id)
    {
        return sslWarmLogMapper.selectSslWarmLogById(id);
    }

    /**
     * 查询消息提醒记录列表
     *
     * @param sslWarmLog 消息提醒记录
     * @return 消息提醒记录
     */
    @Override
    public List<SslNotificationLog> selectSslWarmLogList(SslNotificationLog sslWarmLog)
    {
        return sslWarmLogMapper.selectSslWarmLogList(sslWarmLog);
    }

    /**
     * 新增消息提醒记录
     *
     * @param sslWarmLog 消息提醒记录
     * @return 结果
     */
    @Override
    public int insertSslWarmLog(SslNotificationLog sslWarmLog)
    {
        sslWarmLog.setCreateTime(DateUtils.getNowDate());
        return sslWarmLogMapper.insertSslWarmLog(sslWarmLog);
    }

    /**
     * 修改消息提醒记录
     *
     * @param sslWarmLog 消息提醒记录
     * @return 结果
     */
    @Override
    public int updateSslWarmLog(SslNotificationLog sslWarmLog)
    {
        sslWarmLog.setUpdateTime(DateUtils.getNowDate());
        return sslWarmLogMapper.updateSslWarmLog(sslWarmLog);
    }

    /**
     * 批量删除消息提醒记录
     *
     * @param ids 需要删除的消息提醒记录主键
     * @return 结果
     */
    @Override
    public int deleteSslWarmLogByIds(String ids)
    {
        return sslWarmLogMapper.deleteSslWarmLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除消息提醒记录信息
     *
     * @param id 消息提醒记录主键
     * @return 结果
     */
    @Override
    public int deleteSslWarmLogById(Long id)
    {
        return sslWarmLogMapper.deleteSslWarmLogById(id);
    }

    /**
     * 插入数据
     */
    public void insertLog(String msgType, String domainTxt, String msgTitle, String conentTxt){
        //调用通知
        List<JSONObject> msgParamsList = new ArrayList<>();
        StringBuilder emailContent = new StringBuilder();
        emailContent.append(msgTitle);
        for(String jsonStr:conentTxt.split(",")){
            JSONObject tempJson = new JSONObject();
            String[] tempItem = jsonStr.split("#");
            tempJson.set("labelName", tempItem[0]);
            tempJson.set("color", tempItem[1]);
            tempJson.set("txt", tempItem[2]);
            msgParamsList.add(tempJson);
            emailContent.append("\n"+tempItem[0]+"："+tempItem[2]+"\n");
        }
        //发送企业微信通知
        AjaxResult aj = QyWxWebhookUtils.sendMsg(msgTitle, msgParamsList);
        SslNotificationLog warmLog1 = new SslNotificationLog();
        warmLog1.setReceiveType("qywx");
        warmLog1.setMsgType(msgType);
        warmLog1.setMsgTitle(msgTitle);
        warmLog1.setDomainTxt(domainTxt);
        warmLog1.setMsgContent(conentTxt);
        if(aj.isSuccess()){
            warmLog1.setStatus("0");
        }else{
            warmLog1.setStatus("1");
        }
        //保存数据
        warmLog1.setCreateTime(DateUtils.getNowDate());
        //插入数据
        sslWarmLogMapper.insertSslWarmLog(warmLog1);
        //发送邮箱通知
        aj = EmailUtils.sendEmailMsg(msgTitle, conentTxt, configService.selectConfigByKey("notification.email"));
        warmLog1.setId(null);
        warmLog1.setMsgType(msgType);
        warmLog1.setReceiveType("email");
        if(aj.isSuccess()){
            warmLog1.setStatus("0");
        }else{
            warmLog1.setStatus("1");
        }
        //插入数据
        sslWarmLogMapper.insertSslWarmLog(warmLog1);

    }
}
